<?php

namespace App\Http\Controllers\admin\product;

use App\Http\Controllers\Controller;
use App\Models\order\OrderModel;
use App\Models\product\ProductModel;
use App\Models\product\ProductPopularizeModel;
use App\Models\product\ProductSonModel;
use App\Models\users\UsersModel;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use lxs\api\ApiResponse;
use lxs\tools\Tools;

class OrderController extends Controller
{
    /**
     * 获取订单列表分页页面数据
     */
    public function getOrderPageListData(): JsonResponse
    {
        $data = request()->input();

        $query = OrderModel::with([
            'userInfo' => function ($query) {
                $query->select(['id', 'nickname','mobile', 'avatar']);
            }
        ]);

        if (isset($data['status']) && $data['status'] > 0) {
            match ($data['status']) {
                '1' => $query->where('order_status', 0),
                '2' => $query->where('order_status', 1),
                '3' => $query->where('order_status', 2),
                '4' => $query->where('order_status', 3),
                '5' => $query->where('order_status', 4)
            };
        }

        if (isset($data['order_sn']) && $data['order_sn'] != '') {
            $query->Where('order_sn', 'like', '%'. $data['order_sn']. '%');
        }

        if (isset($data['uid']) && $data['uid'] != '') {
            $query->where('uid', $data['uid']);
        }

        if (isset($data['createTime']) && $data['createTime'] != '') {
            foreach ($data['createTime'] as $k => $v) {
                $data['createTime'][$k] = (new Carbon($v))->format('Y-m-d H:i:s');
            }
            $startTime = Tools::timeConvert($data['createTime'][0]);
            $endTime = Tools::timeConvert($data['createTime'][1]);
            $query->whereBetween('create_time', [$startTime, $endTime]);
        }

        if (isset($data['payTime']) && $data['payTime'] != '') {
            foreach ($data['payTime'] as $k => $v) {
                $data['payTime'][$k] = (new Carbon($v))->format('Y-m-d H:i:s');
            }
            $startTime = Tools::timeConvert($data['payTime'][0]);
            $endTime = Tools::timeConvert($data['payTime'][1]);
            $query->whereBetween('pay_time', [$startTime, $endTime]);
        }

        $orderPageList = $query->orderBy('id', 'desc')->paginate($data['pageSize']);
        foreach ($orderPageList as $item) {
            $item->order_commission_ratio = $item->order_commission_ratio * 100 . '%';
            $productInfo = ProductSonModel::select('product_son.*', 'product.product_title')
                ->leftJoin('product', 'product_son.product_id', '=', 'product.id')
                ->where('product_son.id', $item->product_id)
                ->first();
            if ($productInfo) {
                $popularize_name = ProductPopularizeModel::where('id', $productInfo->popularize_id)->value('popularize_name');
                $item->product_name = $productInfo->product_title . "（{$popularize_name}）";
            } else {
                $item->product_name = '产品不存在';
            }
        }

        return ApiResponse::apiResponseData(200, '获取订单列表分页页面数据成功', [
            'orderPageList' => $orderPageList,
        ]);
    }


    /**
     * 获取订单详情页面数据
     */
    public function getOrderDetailData(): JsonResponse
    {
        $order_id = request()->input('order_id');
        if (empty($order_id)) {
            return ApiResponse::error('订单不存在');
        }

        $query = OrderModel::with(['userInfo']);

        $orderInfo = $query->where('id', $order_id)->first();

        $orderInfo->top_mobile = UsersModel::where('id', $orderInfo->uid)->value('mobile');

        $productInfo = ProductModel::where('id', $orderInfo->product_id)->first();
        $online_platform = ProductModel::getOnlinePlatform($productInfo->online_platform);
        $orderInfo->product_name = $productInfo->product_title . "（{$online_platform}）";
        $orderInfo->product_cover = $productInfo->product_cover;

        return ApiResponse::apiResponseData(200, '获取订单详情页面数据成功', [
            'orderInfo' => $orderInfo,
        ]);
    }


}
